﻿using System;
using System.Text;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;

public partial class Yalie_Yalie : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.FooterStyle.BackColor = System.Drawing.Color.LightGray;
            UpdateRowControl(-1);
        }
    }

    private void UpdateRowControl(int rowIndex)
    {
        if (rowIndex != -1)
        {
            Panel1.Visible = true;
        }
        else
        {
            Panel1.Visible = false;
        }
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow bottomPagerRow = GridView1.BottomPagerRow;
        Label bottomPagerNo = new Label();
        bottomPagerNo.Text = "目前所在分页码(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
        bottomPagerRow.Cells[0].Controls.Add(bottomPagerNo);
    }
    public void GridViewToExcel(Control ctrl, string FileType, string FileName)
    {
        this.ExportData.Visible = false;
        Response.Clear();
        Response.Buffer = true;
        HttpContext.Current.Response.Charset = "GB2312";
        HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;//注意编码
        HttpContext.Current.Response.AppendHeader("Content-Disposition",
            "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 
        ctrl.Page.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        ctrl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void ExportData_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.Style.Clear();
        GridView1.Columns[0].Visible = false;
        GridView1.ShowFooter = false;

        GridView1.DataBind();

        GridViewToExcel(GridView1, "application/ms-excel", "压裂.xls");
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //UpdateRowControl(e.NewSelectedIndex);//显示录入界面
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Roles.IsUserInRole(User.Identity.Name, "admin") || Roles.IsUserInRole(User.Identity.Name, "yalie"))
        {
            txtJH.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
            txtSBRQ.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text.Trim();
            txtSGJDDS1.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[9].Text;
            txtPLYL.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[14].Text;

            string kgrq = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text.Trim();
            if (kgrq.Equals("&nbsp;"))
            {
                this.txtKGRQ.Text = string.Empty;
            }
            else
            {
                this.txtKGRQ.Text = kgrq;
            }
            string wgrq = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text.Trim();
            if (wgrq.Equals("&nbsp;"))
            {
                this.txtWGRQ.Text = string.Empty;
            }
            else
            {
                this.txtWGRQ.Text = wgrq;
            }
            string csmc = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text.Trim();
            if (csmc.Equals("&nbsp;"))
            {
                this.txtCSMC.Text = string.Empty;
            }
            else
            {
                this.txtCSMC.Text = csmc;
            }
            string sgfs = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text.Trim();
            if (sgfs.Equals("&nbsp;"))
            {
                this.txtSGFS.Text = string.Empty;
            }
            else
            {
                this.txtSGFS.Text = sgfs;
            }

            string cscd = GridView1.Rows[GridView1.SelectedIndex].Cells[7].Text.Trim();
            if (cscd.Equals("&nbsp;"))
            {
                this.txtCSCD.Text = string.Empty;
            }
            else
            {
                this.txtCSCD.Text = cscd;
            }

            string sgjdds2 = GridView1.Rows[GridView1.SelectedIndex].Cells[10].Text.Trim();
            if (sgjdds2.Equals("&nbsp;"))
            {
                this.txtSGJDDS2.Text = string.Empty;
            }
            else
            {
                this.txtSGJDDS2.Text = sgjdds2;
            }

            string cwsyhd = GridView1.Rows[GridView1.SelectedIndex].Cells[11].Text.Trim();
            if (cwsyhd.Equals("&nbsp;"))
            {
                this.txtCWSYHD.Text = string.Empty;
            }
            else
            {
                this.txtCWSYHD.Text = cwsyhd;
            }

            string cwyxhd = GridView1.Rows[GridView1.SelectedIndex].Cells[12].Text.Trim();
            if (cwyxhd.Equals("&nbsp;"))
            {
                this.txtCWYXHD.Text = string.Empty;
            }
            else
            {
                this.txtCWYXHD.Text = cwyxhd;
            }

            string jsl = GridView1.Rows[GridView1.SelectedIndex].Cells[13].Text.Trim();
            if (jsl.Equals("&nbsp;"))
            {
                this.txtJSL.Text = string.Empty;
            }
            else
            {
                this.txtJSL.Text = jsl;
            }

            string bz = GridView1.Rows[GridView1.SelectedIndex].Cells[15].Text.Trim();
            if (bz.Equals("&nbsp;"))
            {
                this.txtBZ.Text = string.Empty;
            }
            else
            {
                this.txtBZ.Text = bz;
            }
        }
        else
        {
            UpdateRowControl(-1);

            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert( '对不起，您没有录入压裂数据的权限! ');", true);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //string sbrq = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
        string sbrq = txtSBRQ.Text;
        string kgrq = txtKGRQ.Text;
        string wgrq = txtWGRQ.Text;
        //不加Server.HtmlDecode在更新时会在字符串后面加上&nbsp;
        SqlDataSource1.UpdateParameters.Add("JH", TypeCode.String, txtJH.Text);//主键
        SqlDataSource1.UpdateParameters.Add("SBRQ", TypeCode.DateTime, sbrq);//主键
        SqlDataSource1.UpdateParameters.Add("KGRQ", TypeCode.DateTime, Server.HtmlDecode(kgrq));
        SqlDataSource1.UpdateParameters.Add("WGRQ", TypeCode.DateTime, Server.HtmlDecode(wgrq));
        SqlDataSource1.UpdateParameters.Add("CSMC", TypeCode.String, Server.HtmlDecode(txtCSMC.Text));
        SqlDataSource1.UpdateParameters.Add("SGFS", TypeCode.String, Server.HtmlDecode(txtSGFS.Text));
        SqlDataSource1.UpdateParameters.Add("CSCD", TypeCode.String, Server.HtmlDecode(txtCSCD.Text));
        SqlDataSource1.UpdateParameters.Add("SGJDDS1", TypeCode.Decimal, txtSGJDDS1.Text);//主键
        SqlDataSource1.UpdateParameters.Add("SGJDDS2", TypeCode.Decimal, Server.HtmlDecode(txtSGJDDS2.Text));
        SqlDataSource1.UpdateParameters.Add("CWSYHD", TypeCode.Decimal, Server.HtmlDecode(txtCWSYHD.Text));
        SqlDataSource1.UpdateParameters.Add("CWYXHD", TypeCode.Decimal, Server.HtmlDecode(txtCWYXHD.Text));
        SqlDataSource1.UpdateParameters.Add("JSL", TypeCode.Decimal, Server.HtmlDecode(txtJSL.Text));
        SqlDataSource1.UpdateParameters.Add("PLYL", TypeCode.Decimal, txtPLYL.Text);//主键
        SqlDataSource1.UpdateParameters.Add("BZ", TypeCode.String, Server.HtmlDecode(txtBZ.Text));

        GridView1.UpdateRow(GridView1.SelectedIndex, true);

        UpdateRowControl(-1);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        UpdateRowControl(-1);
    }


}